package com.subject.shuati.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.subject.shuati.application.covert.SubjectAnswerDtoConverter;
import com.subject.shuati.application.covert.SubjectInfoDtoConverter;
import com.subject.shuati.application.dto.SubjectInfoDto;
import com.subject.shuati.common.entity.PageResult;
import com.subject.shuati.common.entity.Result;
import com.subject.shuati.domain.entity.SubjectAnswerBo;
import com.subject.shuati.domain.entity.SubjectInfoBo;
import com.subject.shuati.domain.service.SubjectInfoDomainService;
import com.subject.shuati.infra.basic.entity.SubjectInfoEs;
import com.subject.shuati.infra.basic.service.SubjectEsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 刷题模块控制层
 */
@RestController
@Slf4j
@RequestMapping("/subject")
public class SubjectController {

    @Resource
    private SubjectInfoDomainService domainService;

    @Resource
    private RocketMQTemplate rocketmqTemplate;
    /**
     * 新增题目
     * @param subjectInfoDto
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectInfoDto subjectInfoDto){
        try {
            if (log.isInfoEnabled()){
                log.info("SubjectController.add.dto:{}", JSON.toJSONString(subjectInfoDto));
            }
            Preconditions.checkNotNull(subjectInfoDto.getSubjectName(),"题目名不能为空");
            Preconditions.checkNotNull(subjectInfoDto.getSubjectType(),"题目类型不能为空");
            Preconditions.checkNotNull(subjectInfoDto.getSubjectScore(),"题目分数不能为空");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDto.getCategoryIds()),
                    "题目分类ID不能为空");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDto.getLabelIds()),
                    "题目标签ID不能为空");
            SubjectInfoBo subjectInfoBo = SubjectInfoDtoConverter.INSTANCE.DtoConverterInfoBo(subjectInfoDto);
            List<SubjectAnswerBo> answerBoList = SubjectAnswerDtoConverter
                    .INSTANCE.DtoConverterAnswerBoList(subjectInfoDto.getOptionList());
            subjectInfoBo.setOptionList(answerBoList);
            domainService.add(subjectInfoBo);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("SubjectCategoryController.add.error:{}"+e.getMessage(),e);
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 分页查询题目列表
     */
    @PostMapping("/getSubjectPage")
    public Result<PageResult<SubjectInfoDto>> getSubjectPage(@RequestBody SubjectInfoDto subjectInfoDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.getSubjectPage.dto:{}", JSON.toJSONString(subjectInfoDto));
            }
            Preconditions.checkNotNull(subjectInfoDto.getCategoryId(), "分类id不能为空");
            Preconditions.checkNotNull(subjectInfoDto.getLabelId(), "标签id不能为空");
            SubjectInfoBo subjectInfoBo = SubjectInfoDtoConverter.INSTANCE.DtoConverterInfoBo(subjectInfoDto);
            subjectInfoBo.setPageNo(subjectInfoDto.getPageNo());
            subjectInfoBo.setPageSize(subjectInfoDto.getPageSize());
            PageResult<SubjectInfoBo> boPageResult = domainService.getSubjectPage(subjectInfoBo);
            return Result.ok(boPageResult);
        } catch (Exception e) {
            log.error("SubjectCategoryController.add.error:{}", e.getMessage(), e);
            return Result.fail("分页查询题目失败");
        }
    }

    /**
     * 查询题目详细信息
     */
    @PostMapping("/querySubjectInfo")
    public Result<SubjectInfoDto> querySubjectInfo(@RequestBody SubjectInfoDto subjectInfoDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.querySubjectInfo.dto:{}", JSON.toJSONString(subjectInfoDto));
            }
            Preconditions.checkNotNull(subjectInfoDto.getId(), "题目id不能为空");
            SubjectInfoBo subjectInfoBo = SubjectInfoDtoConverter.INSTANCE.DtoConverterInfoBo(subjectInfoDto);
            SubjectInfoBo boResult = domainService.querySubjectInfo(subjectInfoBo);
            SubjectInfoDto dto = SubjectInfoDtoConverter.INSTANCE.BOConvertToDTO(boResult);
            return Result.ok(dto);
        } catch (Exception e) {
            log.error("SubjectCategoryController.add.error:{}", e.getMessage(), e);
            return Result.fail("查询题目详情失败");
        }
    }

    /**
     * 全文检索
     */
    @PostMapping("/getSubjectPageBySearch")
    public Result<PageResult<SubjectInfoEs>> getSubjectPageBySearch(@RequestBody SubjectInfoDto subjectInfoDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.getSubjectPageBySearch.dto:{}", JSON.toJSONString(subjectInfoDto));
            }
            Preconditions.checkArgument(StringUtils.isNotBlank(subjectInfoDto.getKeyWord()), "关键字Keyword不能为空");
            SubjectInfoBo subjectInfoBo = SubjectInfoDtoConverter.INSTANCE.DtoConverterInfoBo(subjectInfoDto);
            subjectInfoBo.setPageNo(subjectInfoDto.getPageNo());
            subjectInfoBo.setPageSize(subjectInfoDto.getPageSize());
            PageResult<SubjectInfoEs> pageResult = domainService.getSubjectPageBySearch(subjectInfoBo);
            return Result.ok(pageResult);
        } catch (Exception e) {
            log.error("SubjectCategoryController.getSubjectPageBySearch.error:{}", e.getMessage(), e);
            return Result.fail("全文检索失败");
        }
    }

    /**
     * 获取题目贡献榜单
     */
    @PostMapping("/getContributeList")
    public Result<List<SubjectInfoDto>> getContributeList() {
        try {
            List<SubjectInfoBo> boList = domainService.getContributeList();
            List<SubjectInfoDto> dtoList = SubjectInfoDtoConverter.INSTANCE.DtoConverterInfoBoList(boList);
            return Result.ok(dtoList);
        } catch (Exception e) {
            log.error("SubjectCategoryController.getContributeList.error:{}", e.getMessage(), e);
            return Result.fail("获取贡献榜失败");
        }
    }

    /**
     * 测试mq发送
     **/
    @PostMapping("mqtest")
    public Result<Boolean> testMq(@Param("id") int id){
        rocketmqTemplate.convertAndSend("test-topic","我是mq！！！"+id);
        return Result.ok(true);
    }

}
